<template>
  <div class="mod-study__base_user">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="模糊搜索">
        <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('study:user:save')" type="primary"
          @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
      <el-table-column prop="deptId" label="账号所属基地" header-align="center">
        <template v-slot="scope">
          <span>{{ getDeptName(scope.row.deptId) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="研学基地账号名" header-align="center"></el-table-column>
      <el-table-column prop="createDate" label="设置时间" header-align="center"></el-table-column>
      <el-table-column prop="status" label="账号状态" header-align="center">
        <template v-slot="scope">
          <span>{{ scope.row.status === 1 ? '启用' : '停用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('study:user:update')" type="primary" link
            @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button v-if="state.hasPermission('study:user:delete')" type="primary" link
            @click="state.deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="state.hasPermission('study:user:password')" type="primary" link
            @click="updatePasswordHandle(scope.row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, toRefs } from "vue";
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/user/page",
  getDataListIsPage: true,
  deleteURL: "/study/user/del",
  deleteIsBatch: true,
  deleteIsPost: true,
  dataForm: {
    keywords: "",
    type: "BASE"
  }
});
const deptList = ref([]);

const state = reactive({ ...useView(view), ...toRefs(view) });

// 获取基地列表
const getDeptList = () => {
  return baseService.get("/study/dept/page", { type: 'BASE' }).then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    deptList.value = res.data.list;
  });
};

const getDeptName = (val: string) => {
  const dept: any = deptList.value.find((d: any) => d.id === val) || {};
  return dept.name;
};

const addOrUpdateHandle = (row?: any) => {
  router.push({
    path: '/study/base/user/edit',
    query: {
      row: JSON.stringify(row)
    },
  });
};

const updatePasswordHandle = (id: string) => {
  ElMessageBox.prompt('请输入新密码', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: /\S/,
    inputErrorMessage: '新密码不能为空',
  })
    .then(({ value }) => {
      baseService.post('/study/user/update/password', { id, password: value }).then((res) => {
        ElMessage.success({
          message: "密码重置成功",
          duration: 500,
        });
      });
    })
    .catch(() => { })
};

onMounted(() => {
  getDeptList();
});
</script>
